const Pick = require("../template")

function testPickComplexScenarios() {
  console.log(
    "1. 对象选择复杂场景 - 嵌套对象选择:",
    (() => {
      const object = {
        user: { profile: { name: 'John', age: 30, email: 'john@example.com' } },
        settings: { theme: 'dark', lang: 'en' },
        data: { value: 100, nested: { deep: 200 } }
      }
      const result = Pick.pickDeep(object, 'user.profile.name', 'data.nested.deep')
      const expected = {
        user: { profile: { name: 'John' } },
        data: { nested: { deep: 200 } }
      }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "2. 对象选择复杂场景 - 多层嵌套选择:",
    (() => {
      const object = {
        a: { b: { c: { d: { e: 1 } } } },
        f: { g: { h: { i: { j: 2 } } } },
        k: 3
      }
      const result = Pick.pickDeep(object, 'a.b.c.d.e', 'f.g.h.i.j')
      const expected = {
        a: { b: { c: { d: { e: 1 } } } },
        f: { g: { h: { i: { j: 2 } } } }
      }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "3. 对象选择复杂场景 - 混合类型选择:",
    (() => {
      const object = {
        a: 1, b: '2', c: true, d: null, e: undefined,
        f: [1, 2, 3], g: { id: 1 }, h: () => {},
        i: Symbol('test'), j: /test/
      }
      const result = Pick.pickByType(object, 'string')
      const expected = { b: '2' }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "4. 对象选择复杂场景 - 条件组合选择:",
    (() => {
      const object = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }
      const result = Pick.pickBy(object, (value, key) => value > 3 && key !== 'd')
      const expected = { e: 5, f: 6 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "5. 对象选择复杂场景 - 正则模式选择:",
    (() => {
      const object = {
        user_name: 'John',
        user_age: 30,
        user_email: 'john@example.com',
        data_value: 100,
        data_count: 200,
        settings_theme: 'dark'
      }
      const result = Pick.pickByPattern(object, /^user_/)
      const expected = { user_name: 'John', user_age: 30, user_email: 'john@example.com' }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "6. 对象选择复杂场景 - 默认值组合选择:",
    (() => {
      const object = { a: 1, b: 2, c: 3, d: 4, e: 5 }
      const result = Pick.pickWithDefaults(object, 'a', 'c', { b: 0, d: 0, f: 0 })
      const expected = { a: 1, b: 0, c: 3, d: 0, f: 0 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "7. 对象选择复杂场景 - 转换组合选择:",
    (() => {
      const object = { a: 1, b: 2, c: 3, d: 4, e: 5 }
      const result = Pick.pickWithTransform(object, 'a', 'c', {
        b: x => x * 2,
        d: x => x * 3,
        e: x => x * 4,
        f: x => x * 5
      })
      const expected = { a: 1, b: 4, c: 3, d: 12, e: 20, f: 25 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "8. 对象选择复杂场景 - 验证组合选择:",
    (() => {
      const object = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }
      const result = Pick.pickWithValidation(object, 'a', 'c', { min: 1, max: 10 })
      const expected = { a: 1, c: 3 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "9. 对象选择复杂场景 - 特殊值处理:",
    (() => {
      const object = {
        a: NaN, b: Infinity, c: -Infinity, d: 0, e: -0,
        f: '', g: ' ', h: null, i: undefined
      }
      const result = Pick.pick(object, 'a', 'b', 'c')
      const expected = { a: NaN, b: Infinity, c: -Infinity }
      return Object.keys(result).length === 3
    })()
  )

  console.log(
    "10. 对象选择复杂场景 - 大数处理:",
    (() => {
      const object = {
        a: Number.MAX_SAFE_INTEGER,
        b: Number.MIN_SAFE_INTEGER,
        c: 9007199254740991,
        d: -9007199254740991
      }
      const result = Pick.pick(object, 'a', 'b')
      const expected = { a: Number.MAX_SAFE_INTEGER, b: Number.MIN_SAFE_INTEGER }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "11. 对象选择复杂场景 - 函数选择:",
    (() => {
      const fn1 = () => {}
      const fn2 = () => {}
      const object = { a: fn1, b: fn2, c: 1, d: 2 }
      const result = Pick.pickByType(object, 'function')
      const expected = { a: fn1, b: fn2 }
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "12. 对象选择复杂场景 - 正则表达式选择:",
    (() => {
      const regex1 = /test/g
      const regex2 = /test/i
      const object = { a: regex1, b: regex2, c: 1, d: 2 }
      const result = Pick.pickByType(object, 'object')
      const expected = { a: regex1, b: regex2 }
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "13. 对象选择复杂场景 - 日期对象选择:",
    (() => {
      const date1 = new Date('2023-01-01')
      const date2 = new Date('2023-01-02')
      const object = { a: date1, b: date2, c: 1, d: 2 }
      const result = Pick.pick(object, 'a', 'b')
      const expected = { a: date1, b: date2 }
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "14. 对象选择复杂场景 - 循环引用对象:",
    (() => {
      const obj1 = { id: 1 }
      const obj2 = { id: 2 }
      obj1.ref = obj1
      obj2.ref = obj2
      const object = { a: obj1, b: obj2, c: 1 }
      const result = Pick.pick(object, 'a', 'b')
      const expected = { a: obj1, b: obj2 }
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "15. 对象选择复杂场景 - 异步选择:",
    (() => {
      const object = { a: 1, b: 2, c: 3, d: 4, e: 5 }
      return Pick.pickAsync(object, 'a', 'c', async (value, key) => value * 2).then(result => {
        const expected = { a: 2, c: 6 }
        return JSON.stringify(result) === JSON.stringify(expected)
      })
    })()
  )

  console.log(
    "16. 对象选择复杂场景 - 并发选择:",
    (() => {
      const object = { a: 1, b: 2, c: 3, d: 4, e: 5 }
      return Pick.pickConcurrent(object, 'a', 'c', async (value, key) => value * 2, 3).then(result => {
        const expected = { a: 2, c: 6 }
        return JSON.stringify(result) === JSON.stringify(expected)
      })
    })()
  )

  console.log(
    "17. 对象选择复杂场景 - 统计选择:",
    (() => {
      const object = { a: 1, b: 2, c: 3, d: 4, e: 5 }
      const result = Pick.pickWithStats(object, 'a', 'c')
      const expected = { a: 1, c: 3 }
      return JSON.stringify(result.result) === JSON.stringify(expected) && typeof result.stats === 'object'
    })()
  )

  console.log(
    "18. 对象选择复杂场景 - 边界条件:",
    (() => {
      const object = { a: null, b: undefined, c: null, d: undefined }
      const result = Pick.pick(object, 'a', 'b')
      const expected = { a: null, b: undefined }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "19. 对象选择复杂场景 - 性能边界:",
    (() => {
      const largeObject = {}
      for (let i = 0; i < 100000; i++) {
        largeObject[`prop${i}`] = i
      }
      const start = performance.now()
      const result = Pick.pick(largeObject, 'prop0', 'prop1', 'prop2')
      const end = performance.now()
      return (end - start) < 1000 && Object.keys(result).length === 3 // 10万条数据应该在1秒内完成
    })()
  )

  console.log(
    "20. 对象选择复杂场景 - 内存压力:",
    (() => {
      const largeObject = {}
      for (let i = 0; i < 50000; i++) {
        largeObject[`prop${i}`] = { id: i, data: { value: i, nested: { deep: i % 10 } } }
      }
      const start = performance.now()
      const result = Pick.pickDeep(largeObject, 'prop0.data.value', 'prop1.data.value')
      const end = performance.now()
      return (end - start) < 500 && Object.keys(result).length === 1
    })()
  )
}

testPickComplexScenarios()
